package com.qing.administer.uitl;


import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

//代码自动生成器
public class CodeGenerator {

	//表名
	private static final String[] tableNames=new String[]{"sys_config","sys_log","sys_menu","sys_role","sys_role_menu","sys_user","sys_user_role"};
	//数据库连接信息
	private static  final String driverName="com.mysql.cj.jdbc.Driver";
	private static  final String url="jdbc:mysql://1.117.113.243:3306/administer?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
	private static  final String name="root";
	private static  final String password="123456";

	public static void main(String[] args) {
		//构建一个代码自动生成器对象
		AutoGenerator mpg=new AutoGenerator();

		//1. 配置策略
		GlobalConfig gc=new GlobalConfig();
		String projectPath=System.getProperty("user.dir");
		gc.setOutputDir(projectPath+"/src/main/java");
		gc.setAuthor("qing");
		gc.setOpen(false);
		gc.setFileOverride(false); //是否覆盖
		gc.setServiceName("%sService");//去Service的I前缀
		gc.setIdType(IdType.ASSIGN_ID);
		gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
		gc.setSwagger2(true);

		//2. 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl(url);
		// dsc.setSchemaName("public");
		dsc.setDriverName(driverName);
		dsc.setUsername(name);
		dsc.setPassword(password);


		// 3.包配置
		PackageConfig pc = new PackageConfig();
		pc.setModuleName("administer"); //模块名
		pc.setParent("com.qing");
		pc.setEntity("entity");
		pc.setController("controller");
		pc.setService("service");
		pc.setMapper("mapper");
		pc.setXml("mapper.xml");


		// 4. 策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setNaming(NamingStrategy.underline_to_camel); //下划线转驼峰
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);

		strategy.setEntityLombokModel(true);  //是否支持链式编程
		strategy.setRestControllerStyle(true); //Rest风格驼峰命名
		strategy.setControllerMappingHyphenStyle(true); //@RestMapping 驼峰命名

		// 公共类
		//strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
//		strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
//		strategy.setSuperEntityColumns("id");  // 写于父类中的公共字段

		strategy.setInclude(tableNames); //表名，多个英文逗号分割
//		strategy.setTablePrefix(pc.getModuleName() + "_");

		//乐观锁
		strategy.setVersionFieldName("version");
		//逻辑删除
		strategy.setLogicDeleteFieldName("deleted");
		//自动填充配置
		TableFill create=new TableFill("create_time", FieldFill.INSERT);
		TableFill update=new TableFill("update_time",FieldFill.INSERT_UPDATE);
		List<TableFill> tableFillList=new ArrayList<>();
		tableFillList.add(create);
		tableFillList.add(update);
		strategy.setTableFillList(tableFillList);


		mpg.setStrategy(strategy);
		mpg.setPackageInfo(pc);
		mpg.setGlobalConfig(gc);
		mpg.setDataSource(dsc);
		mpg.execute();//执行

	}
}